#ifndef BMSIO_H
#define BMSIO_H

#include <QThread>
#include <QProcess>
#include "xnserial.h"
//#include "ComStruct.h"
//#include "rtdb.h"
#include  "KgGlobalData.h"

// Read IO from Dev(HMI170-7) // 2024-03-27
class CBMSIO : public QThread
{
	Q_OBJECT

public:
	CBMSIO(QObject *parent = 0);
	~CBMSIO();

public:
	void run();
	int init();
	//CIOModule* getIOModule();

private:	
	void printSendInfo(char* pDataBuf, int iCmdSendNum);
	void printReceInfo(char* pDataBuf, int iLen);

	int formDatagram(int iType, int iModbusID, int iDOIndex = 0, quint16 usDOCtrlVal = 0);
	int analyDatagram(int iType, int iModbusID, char* pDataBuf, int iLen);
	void readDataFromSerial();

	int prepareExcepDatagram(uchar uaExcepCode);

	int addParam2Buffer(char* pData, quint16 uiParamVal);
	int getParamFromBuffer(char* pData, quint16& uiParamVal);
	int addModbusCRC2Buffer(char *pData, int iLen);
	quint16 Modbus_CRC16(char *pData, quint16 usLen);
	
private:
	CXnSerial* m_pSerial;
	//CRTDB* m_pRTDB;
	CIOModule* m_pIOModule;

	bool m_bSerialOK;
	QString m_strPortName; // 串口名
	int m_iBaudRate;       // 波特率
	int m_iDataBits;       // 数据位
	int m_iStopBits;       // 停止位
	int m_iParity;         // 校验位	
	int m_iRtscts;

	bool m_bPrintMsg;
	int m_iSerialDataLen;
	char* m_pSerialDataBuf;   // data buffer
	int  m_logCount;
};

#endif // IO_H
